<?php

/**
 * HttpResponse class
 *
 * @author vbolshov
 */

namespace km;

class HttpResponse {
	/**
	 * @var string
	 */
	private $status = '200 OK', 
		$content_type = 'text/html',
		$charset = 'utf-8',
		$language = 'en',
		$last_modified;
	/**
	 * @var array
	 */
	private $headers = array();
	/**
	 * @param string $contentType
	 */
	function setContentType($content_type)
	{
		$this->content_type = $content_type;
	}
	/**
	 * @return string
	 */
	function getContentType()
	{
		return $this->content_type;
	}
	
	/**
	 * @param string $language
	 */
	function setLanguage($language)
	{
		$this->language = $language;
	}
	/**
	 * @return string
	 */
	function getLanguage()
	{
		return $this->language;
	}
	
	/**
	 * @param string $status
	 */
	function setStatus($status)
	{
		$this->status = $status;
	}
	/**
	 * Specify arbitrary HTTP header
	 *
	 * @param string $header
	 */
	function header($header)
	{
		$this->headers[] = $header;
	}
	/**
	 * Send HTTP headers
	 *
	 */
	function sendHeaders()
	{
		$this->exposeStatus();
		$this->exposeContentType();
		$this->exposeLanguage();
		$this->exposeLastModified();
		foreach ($this->headers as $header) {
			header($header);
		}
	}
	
	/**
	 * Send status header
	 */
	function exposeStatus()
	{
		header("Status: " . $this->status, true, (int) $this->status);
	}
	
	/**
	 * @param string $charset
	 */
	function setCharset($charset)
	{
		$this->charset = $charset;
	}
	
	/**
	 * @return string
	 */
	function getCharset()
	{
		return $this->charset;
	}
	
	/**
	 * @param string $datetime
	 */
	function setLastModified($datetime)
	{
		if (($time = strtotime($datetime)) > $this->last_modified)
		{
			$this->last_modified = $time;
		}
	}
	
	/**
	 * Send Content-Type header
	 */
	function exposeContentType()
	{
		header("Content-Type: " . $this->content_type . "; charset=" . $this->charset);
	}
	
	/**
	 * Send Content-Language header
	 */
	function exposeLanguage()
	{
		header("Content-Language: " . $this->language);
	}
	
	/**
	 * Send Last-Modified header
	 */
	function exposeLastModified()
	{
		if (null === $this->last_modified) 
		{
			return;
		}
		header("Last-Modified: " . gmdate('D, d M Y H:i:s', $this->last_modified) . " GMT");
	}
}